package Main;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.io.File;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.FloatControl;

/**
 *
 * @author Huy
 */
public class SoundClip extends Sound{

	//AudioInputStream soundStream;
	FloatControl gainControl;
	Clip clip;

	public SoundClip(String fileName) {
		try {
			AudioInputStream soundStream = AudioSystem.getAudioInputStream(new File(fileName));
			clip = AudioSystem.getClip();
			clip.open(soundStream);
			gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
		} catch (Exception ex) {
			clip = null;
		}
	}

	@Override
	public void start() {
		if (clip != null && !clip.isRunning()) {
			clip.loop(1);
		}
	}

	@Override
	public void setVolume(float f) {
		if(gainControl!=null)
			gainControl.setValue(f);
	}

	@Override
	public void loop() {
		if (clip != null && !clip.isRunning()) {
			clip.loop(Clip.LOOP_CONTINUOUSLY);
		}
	}

	@Override
	public void stop() {
		if (clip != null) {
			clip.stop();
		}
	}
	
}
